package Exercise;

public class Test1ComprehensivePassword2 {
    public static int[] originalPassWord(int passWord) {
        int temp1 = passWord, temp2, i = 0, length = 0;
        while (temp1 != 0) {
            temp1 /= 10;
            length++;
        }
        int[] arr = new int[length];
        temp1 = passWord;
        while (temp1 != 0) {
            temp2 = temp1 % 10;
            temp1 /= 10;
            arr[i] = temp2;
            i++;
        }
        return arr;
    }

    public static void encrypt(int[] passWordArr) {
        for (int i = 0; i < passWordArr.length; i++) {
            passWordArr[i] = (passWordArr[i] + 5) % 10;
        }
    }

    public static void unencrypt(int[] passWordArr) {
        for (int i = 0; i < passWordArr.length; i++) {
            if (passWordArr[i] >= 0 && passWordArr[i] <= 4) {
                passWordArr[i] += 10;
            }
            passWordArr[i] -= 5;
        }

    }

    public static int arrToInt(int[] passWordArr) {
        int passWord = 0;
        for (int i = passWordArr.length - 1; i >= 0; i--) {
            passWord = passWord * 10 + passWordArr[i];
        }
        return passWord;
    }

    public static void main(String[] args) {
        int passWord = 239816;
        int[] passWordArr = originalPassWord(passWord);
        encrypt(passWordArr);
        passWord = arrToInt(passWordArr);
        System.out.println(passWord);
        unencrypt(passWordArr);
        passWord = arrToInt(passWordArr);
        System.out.println(passWord);
    }
}
